package com.xiaoq.hotel.repository.impl;

import com.xiaoq.coms.util.IdGeneratorUtils;
import com.xiaoq.hotel.entity.HotelEntity;
import com.xiaoq.hotel.repository.IHotelRepository;
import com.xiaoq.sites.entity.SitesEntity;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;

import java.util.List;

@Repository
public class HotelRepositoryImpl implements IHotelRepository {

    @Autowired
    private Datastore dataStore;


    @Override
    public void saveHotel(HotelEntity hotel) {
        dataStore.save(hotel);
    }

    @Override
    public List<HotelEntity> findHotel(String cityCode) {
        List<HotelEntity> hotels = dataStore.find(HotelEntity.class).field("zoneCity").equal(cityCode).field("delTag").equal("10").asList();
        return hotels;
    }

    @Override
    public List<HotelEntity> findHotel(String cityCode, String recommend) {
        List<HotelEntity> hotels = dataStore.find(HotelEntity.class).field("zoneCity").equal(cityCode).field("recommend").equal(recommend).asList();
        return hotels;
    }

    @Override
    public HotelEntity findHotelDetail(String id) {
        HotelEntity hotels = dataStore.find(HotelEntity.class).field("_id").equal(id).get();
        return hotels;
    }

    @Override
    public List<HotelEntity> findShopFoodList(Double longitude, Double latitude, Double localLongitude, Double localLatitude, PagingVo pvo) {
        Double dislongitude = 0.02;
        Double dislatitude = 0.02;
        List<HotelEntity> shopFoodList = null;


        Long total = 0L;
        while (true) {
            shopFoodList = new ArrayList<>();
            Double longitudeAddtion = longitude + dislongitude;
            Double longitudeLessen = longitude - dislongitude;
            Double latitudeAddtion = latitude + dislatitude;
            Double latitudeLessen = latitude - dislatitude;
            Query<HotelEntity> hotelQuery = dataStore.find(HotelEntity.class);
            hotelQuery.filter("longitude <", longitudeAddtion).filter("longitude >", longitudeLessen)
                    .filter("latitude >", latitudeLessen).filter("latitude <", latitudeAddtion);
//                    .order("-avgScore");
            total = hotelQuery.count();

            if(total > 0){
                if(total >= 90 ){
                    shopFoodList = hotelQuery.asList();
                    break;
                }
                if (dislongitude >= 0.06){
                    shopFoodList = hotelQuery.asList();
                    break;
                }
            }

            dislongitude = dislongitude + 0.02;
            dislatitude = dislatitude + 0.02;
            // shopFoodQuery.field("loc").near(localLongitude, localLatitude);
            // shopFoodQuery.offset(pn).limit(ps);

        }
        pvo.setTc(total);
        return shopFoodList;
    }

    @Override
    public List<HotelEntity> fuzzyByName(String name,String cityCode, PagingVo pagingVo) {
        Query<HotelEntity> hotelQuery = dataStore.createQuery(HotelEntity.class);
        hotelQuery.field("name").contains(name);
        hotelQuery.field("zoneCity").equal(cityCode);
        if (pagingVo != null){
            hotelQuery.offset(pagingVo.getPageOffset()).limit(pagingVo.getPs());
        }
        return hotelQuery.asList();
    }

    @Override
    public void delete(String id) {
        Query<HotelEntity> hotleQuery = dataStore.find(HotelEntity.class);
        hotleQuery.field(IdGeneratorUtils.IDGENERATOR).equal(id);
        dataStore.delete(hotleQuery);

    }

    @Override
    public List<HotelEntity> findHotel(String cityCode, String delTag,PagingVo pvo) {
        int ps = pvo.getPs();
        int pn = (pvo.getPn() - 1) * ps;
        Query<HotelEntity> hotelQuery = dataStore.createQuery(HotelEntity.class);
        if(StringUtils.isNotBlank(delTag)){
            hotelQuery.field("delTag").equal(delTag);
        }
        hotelQuery.field("zoneCity").equal(cityCode);
        Long total = 0L;
        total = hotelQuery.count();
        pvo.setTc(total);
        hotelQuery.order("sort").offset(pn).limit(ps);
        return hotelQuery.asList();
    }
}
